use actix_web::{App, HttpServer};
use codec::{device_session, event, user_session};

use crate::{
    config::{HTML_DIR, WEB_ADDR},
    error::AppErr,
};

mod api;
pub mod cfg;
mod codec;

pub use codec::download;

pub async fn init() -> Result<(), AppErr> {
    device_session::init();
    user_session::init();
    event::init();

    let app = || {
        let fs = actix_files::Files::new("/web", HTML_DIR).index_file("index.html");

        App::new().service(fs).configure(api::config)
    };

    println!("web addr: {}", WEB_ADDR);

    HttpServer::new(app).bind(WEB_ADDR)?.run().await?;

    Ok(())
}
